home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12796 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  56 lines

  1. Path: news3.digex.net!usenet
  2. From: Scott Stanchfield <scooter@mccabe.com>
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Re: Q: '\n' character
  5. Date: Tue, 02 Apr 1996 16:53:27 -0500
  6. Organization: McCabe & Associates
  7. Message-ID: <3161A1D7.2168@mccabe.com>
  8. References: <31616F63.481D@lava.weeg.uiowa.edu>
  9. NNTP-Posting-Host: mccabe.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 3.0b2 (X11; I; HP-UX A.09.05 9000/720)
  14.  
  15. If this is a homework problem, press "delete" now...
  16.  
  17. Spoiler alert...  ;)
  18.  
  19.  
  20.  
  21. How about:
  22.  
  23.     char text[MY_BUF_LEN];
  24.     char *pos;
  25.  
  26.     if (fgets(text, MY_BUF_LEN, file))
  27.         if (pos = strchr(text, '\n'))
  28.             *pos = '\0';
  29.  
  30. which will get the line of text (assuming it is definitely shorter than
  31. MY_BUF_LEN -- if not, this gets a bit trickier but is left as an
  32. exercise for the interested reader...), find the first newline (it will
  33. end the string if present), then overwrite it with an end-of-string
  34. null.
  35.  
  36. -- Scott
  37.  
  38.  
  39. Artur Wojdat wrote:
  40. > Hello everybody,
  41. >         Is there a function or some sort of way that I could remove '\n'
  42. > charecter form the end of the string. I'm reading from two files, want to
  43. > form one line of text and then have it printed out to stdout. I use fgets to
  44. > read from the file and I noticed that it appends newline char at the end.
  45. >         It is important that two lines of text, one from each file, will be
  46. > combined into one and I can't do it because the first string has '\n' added
  47. > to it. I'm picky becauuse the output will be used to feed another program and
  48. > I'm affraid that not properly formatted input may corrupt the process.
  49. >         Any suggestions will be greatly appreciated .. Thanks, Art ...
  50.  
  51. -- 
  52. Scott Stanchfield     McCabe & Associates -- Columbia, Maryland
  53. (These are not necessarily the opinions of McCabe & Associates)
  54.       Visit McCabe & Associates at http://www.mccabe.com
  55.